<?php

class Link_Api_Link
{

	function addNew($resource, $poster, $data)
	{
		$table = Nuo::getTable('link');
		$item = $table -> fetchNew();
		$item -> resource_id = $resource -> getItemId();
		$item -> poster_id = $poster -> getItemId();
		$item -> title = $data['title'];
		$item -> url = $data['url'];
		$item -> host = $data['host'];
		$item -> description = $data['description'];
		$item -> thumbnail = $data['thumbnail'];
		$item -> created_at = time();
		$item -> save();

		return $item;
	}

	function getInfo($url)
	{
		$headers = get_headers($url, $format = 1);

		$contentType = isset($headers['Content-Type']) ? $headers['Content-Type'] : 'text/html';

		$contentType = explode(';', $contentType);

		$contentType = array_shift($contentType);

		$contentType = trim($contentType);

		$contentType = strtolower($contentType);

		switch($contentType)
		{
			case 'text/plain' :
				break;
			case 'text/html' :
				return $this -> parseEmbed($url);
				break;
			case 'text/xml' :
				break;
			default :
				return FALSE;
		}

	}

	function parseEmbed($url)
	{
		$return = array();
		
		$urlInfo = parse_url($url);
		
		$host  = $urlInfo['host'];

		$q = array(
			'key' => '4818ad690bb94818bede02dae61d3e72',
			'url' => $url,
			'maxwidth' => 500,
		);

		$apiLink = "http://api.embed.ly/1/oembed?" . http_build_query($q, NULL, '&');

		$curl = new Nuo_Curl();

		$response = $curl -> send($apiLink);

		$data = json_decode($response, 1);

		if (!$data)
		{
			return NULL;
		}

		$return  = array(
			'title' => $data['title'],
			'description' => $data['description'],
			'images' => array($data['thumbnail_url']),
			'thumbnail' => $data['thumbnail_url'],
			'host'=>$host,
			'url'=>$url,
		);

		$return['image_count'] = count($return['images']);

		return $return;
	}

	function parseHtml($url)
	{
		$urlInfo = parse_url($url);

		$baseUrl = $urlInfo['scheme'] . '://' . $urlInfo['host'];

		$curl = new Nuo_Curl();

		$HTML = $curl -> send($url);

		$doc = new DOMDocument();

		$doc -> loadHTML($HTML);

		$metas = array();
		$imgs = array();
		$title = '';

		$metaTags = $doc -> getElementsByTagName('meta');

		foreach ($metaTags as $metaTag)
		{
			$k = $metaTag -> getAttribute('name');

			if (!$k)
			{
				$k = $metaTag -> getAttribute('property');
			}
			if (!$k)
			{
				continue;
			}
			$k = strtolower($k);
			$metas[$k] = $metaTag -> getAttribute('content');
		}

		$imgTags = $doc -> getElementsByTagName('img');

		$imgs = array();

		$titleTags = $doc -> getElementsByTagName('title');

		foreach ($titleTags as $titleTag)
		{
			$title = (string)$titleTag -> nodeValue;
		}

		$imgTypes = array(
			'jpg' => 1,
			'gif' => 1,
			'jpeg' => 1,
			'png' => 1
		);

		foreach ($imgTags as $imgTag)
		{
			$src = $imgTag -> getAttribute('src');

			$pos = strrpos($src, '.');

			if ($pos < 1)
			{
				continue;
			}

			$ext = strtolower(substr($src, $pos + 1));

			if (!isset($imgTypes[$ext]))
			{
				continue;
			}

			if (strpos($src, '/') === 0)
			{
				$src = $baseUrl . "/" . ltrim($src, '/');
			}
			else
			if (strpos($src, './') === 0)
			{
				// If relative to current path, add baseUrl
				$src = $baseUrl . '/' . substr($src, 2);
			}

			list($w, $h) = @getimagesize($src);

			$imgs[] = $src;
		}

		return array(
			//'metas' => $metas,
			'title' => trim($title),
			'description' => trim($metas['description']),
			'host' => $urlInfo['host'],
			'baseUrl' => $baseUrl,
			'link' => $url,
			'images' => $imgs,
		);
	}

}
